package com.ericsson.epc.modules.ebmAutoCollector.task;

import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.epc.modules.ebmAutoCollector.entity.EbmAutoCInfo;
import com.ericsson.epc.modules.ebmAutoCollector.entity.EbmAutoPInfo;
import com.ericsson.epc.modules.ebmAutoCollector.entity.Net;
import com.ericsson.epc.modules.ebmAutoCollector.service.EbmService;
import com.ericsson.epc.modules.ebmAutoCollector.service.NetService;
import com.ericsson.epc.modules.ebmAutoCollector.thread.EbmSignalThread;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @description: ebm自动采集任务
 * @author: wangjingshi
 * @create: 2021-02-22 09:41
 */
@Component
public class EbmAutoCollectorTask implements ManualTask {

    private Logger log = LoggerUtils.Logger(LogFileName.EbmAutoCollectorJob);
    @Autowired
    private EbmService ebmService;
    @Autowired
    private NetService netService;

    @Override
    public void execute() {
        log.info(" ==== task start ==== "+ DateUtils.getDateTime());
        List<EbmAutoPInfo> plist = ebmService.getConfigList();
        Map<String,Object> paramMap = ebmService.queryParamMap();
        for (EbmAutoPInfo p:plist) {
            String startTime = p.getStarttime();
            String endTime = p.getEndtime();
            String nowTime = DateUtils.getDateTime();
            int mnoh = p.getMnoh();
            String lcpt = p.getLcpt();
            System.out.println(lcpt);
            log.info(" ==== nowTime:"+nowTime+" cc:"+p.getCc()+" scc"+p.getScc()+" Between "+startTime + " and " + endTime);
            //判断当前数据是否在有效期
            if(nowTime.compareTo(startTime)>=0 && nowTime.compareTo(endTime)<0){
                List<EbmAutoCInfo> clist = ebmService.getEbmAutoCList(p.getId());
                for(EbmAutoCInfo c:clist){
                    Net net = netService.get(c.getNetid());
                    log.info("pid:"+p.getId()+" cc:"+p.getCc()+" scc:"+p.getScc()+" netid:"+c.getNetid());
                    new Thread(new EbmSignalThread(net,c,p.getCc(),p.getScc(),paramMap,ebmService,endTime,p.getId(),mnoh,lcpt),
                            "pid:"+p.getId()+" cc:"+p.getCc()+" scc:"+p.getScc()+" netid:"+c.getNetid()).start();
                }
            }
        }
    }
}
